<?php
class user_tracking extends Zcontroller {
	
	function __construct() {
		parent::__construct();
		Zsession::setDataToSession("current_page", "home");	
        $this->load_translation(array("job"));
        $this->load_translation(array("classified"));        
	}
	
	public function index()
	{ 
		$this->load->library('pagination');
		$config['base_url'] = site_url("/user_tracking/index");
		$config['total_rows'] = $this->db->count_all('hits_counter');
		$config['per_page'] = '11';
		$config['num_links'] = '10';
		$config['full_tag_open'] = '<p>';
		$config['full_tag_close'] = '</p>';
		$this->pagination->initialize($config);

		$ip = $this->input->ip_address();
	
		$this->load->model("m_usertracking");
		$data['visitorDate']=$this->m_usertracking->getVisitorAll($config['per_page'],$this->uri->segment(3));
		$data['visitorYesterday']=$this->m_usertracking->getVisitorYesterday();
		$data["visitorLastWeek"]=$this->m_usertracking->getVisitorLastWeek();
		
		$data["visitorToday"]=$this->m_usertracking->getVisitorToday();
		$data["visitorLastMonth"]=$this->m_usertracking->getVisitorLastMonth();
		$this->load->view("user_tracking/userTracking",$data);
	}
	
	function getVisitorDate()
	{	
		$this->load->model("m_usertracking");
		$this->load->library('pagination');
		$startDate = $_POST['startDate'];
		$endDate = $_POST['endDate'];
		
		//if($startDate==''){$startDate = date("m/d/Y");}
		if($endDate==''){$endDate = date("m/d/Y");}
		isset($startDate)=="" ? $startDate : date("m/d/Y");
		$config['base_url'] = site_url("/user_tracking/getVisitorDate");
		$num_row = $this->m_usertracking->getVisitor_total_rows($_POST['startDate'],$_POST['endDate']);
		$config['total_rows'] = $num_row;
				
		$config['per_page'] = '10';
		$config['num_links'] = '8';
		$config['full_tag_open'] = '<p>';
		$config['full_tag_close'] = '</p>';
		$this->pagination->initialize($config);	
				
		
		$data["rowNum"]=$num_row;
		$data["visitorDate"]=$this->m_usertracking->getVisitorDate($startDate,$endDate,$config['per_page'],$this->uri->segment(3));

		$data['visitorYesterday']=$this->m_usertracking->getVisitorYesterday();
		$data["visitorLastWeek"]=$this->m_usertracking->getVisitorLastWeek();
		$data["visitorToday"]=$this->m_usertracking->getVisitorToday();
		$data["visitorLastMonth"]=$this->m_usertracking->getVisitorLastMonth();
		$this->load->view("user_tracking/userTracking",$data);
	}
	
	function ads($company){
		$ip = $this->input->ip_address();
		// Check for previous visits
		$query = $this->db->get_where('ads_tracking', array('ip' => $ip, 'company_name' => $company));
		$query = $query->row_array();
		
		if (count($query) < 1)
		{
			    // Never visited - add
			    $date = new DateTime();
			    //$company ='zaman';
			    $this->db->insert('ads_tracking', array('ip' => $ip, 'company_name' => $company, 'hits_date' => $date->format('Y-m-d H:i:s')) );
		} 
		
		redirect('http://www.'.$company, 'refresh');
	}
	
	function company_ads(){
		$this->load->model("m_usertracking");
		
		//$data['visitorDate']=$this->m_usertracking->getVisitorAll($config['per_page'],$this->uri->segment(3));
		$data['visitorYesterday']=$this->m_usertracking->getVisitorYesterday();
		$data["visitorLastWeek"]=$this->m_usertracking->getVisitorLastWeek();
		
		$data["visitorToday"]=$this->m_usertracking->getVisitorToday();
		$data["visitorLastMonth"]=$this->m_usertracking->getVisitorLastMonth();
		
		$data["hit_com_ads"] = $this->m_usertracking->hit_com_ads_id();
		//$data["hit_com_id"] = $this->m_usertracking->hit_com_ads_id(1);
		//echo $this->m_usertracking->hit_com_ads_id(1);
		$this->load->view("user_tracking/hit_com_ads", $data);
	}
	
	function hit_ads($id,$url){
		$this->load->model("m_usertracking");
		$this->m_usertracking->insert_com_ads($id);
		redirect("http://www.".$url);
	}
	
	function company(){
		$this->load->model("m_usertracking");
		
		$data['visitorYesterday']=$this->m_usertracking->getVisitorYesterday();
		$data["visitorLastWeek"]=$this->m_usertracking->getVisitorLastWeek();
		
		$data["visitorToday"]=$this->m_usertracking->getVisitorToday();
		$data["visitorLastMonth"]=$this->m_usertracking->getVisitorLastMonth();
		
		$data["get_company"] = $this->m_usertracking->get_company();
		$this->load->view("user_tracking/company", $data);
		
	}
	
	
	function hit_count($ads_id, $url=NULL){
		
		$this->load->model("m_usertracking");
		$this->m_usertracking->insert_hit_count($ads_id);
		$url = isset($url) && $url!="" ? "http://www.".$url : $_SERVER['HTTP_REFERER'];
		redirect($url);
	}
		

}

?>
